<?php
App::uses('AppController', 'Controller');

/**
 *author quoctran
 * 
 */
class ProductsController extends AppController {
	var $name = "Products";// ten cua Controller
    var $helpers = array('Paginator','Html');
    var $components = array('Session','Files', 'Link');
    var $paginate = array();
	public function beforeFilter() {
		parent::beforeFilter();
	}
	/**
	 * index method
	 */
	public function admin_index() {
		$conditions = array("Product.deleted" => 0);
		$this->paginate = array(
                                'limit' => ITEM_PER_PAGE,
                                'order' => array('id' => 'asc'),
                             );
        $data = $this->paginate("Product",$conditions);
        $this->set("data",$data);
	}
	/**
	 * add method
	 */
	public function admin_add() {
		
		$this->loadModel('Os');
		$os = $this->Os->find('all');
		$this->set("os",$os);
		
		if ($this -> request -> is('post')) {
			$new = $this -> data;
			$new['deleted'] = 0;
			$new['updateAt']= date("Y-m-d H:i:s"); 
			if ($this->Files->isValidFileType($this->data['img1']['name'])){
				$new['img1'] = $this->Files->uploadFile(DIR_PRODUCT, $this->data['img1']['tmp_name'], IMG_PRODUCT, $this->data['img1']['name']);
				$new['img2'] = $this->Files->uploadFile(DIR_PRODUCT, $this->data['img2']['tmp_name'], IMG_PRODUCT, $this->data['img2']['name']);
				$new['icon'] = $this->Files->uploadFile(DIR_PRODUCT, $this->data['icon']['tmp_name'], IMG_PRODUCT, $this->data['icon']['name']);
				$new['shortLink'] = $this->Link->getShortLink($new['name']);
				if ($this -> Product -> save($new)){
					$this->Session->setFlash('Sảm phẩm "'.$new['name'].'" thêm thành công');
					$this->redirect(array('action' => 'index'));
				}
			}else{
				$this->Session->setFlash('file type');
			}
			$this->set('errors', $this->Product->validationErrors);
		}
	}

	/**
	 * edit method
	 * param id integer , id of product
	 */
	public function admin_edit($id) {
		$this->loadModel('Os');
		
		$os = $this->Os->find('all');
		$this->set("os",$os);
		
		$conditions = array("Product.id" => $id);
		$data = $this->Product->find('first', array('conditions' => $conditions));
		parent::createException($data['Product']);
		$this -> set("data", $data['Product']);
		$this -> set("id",$id);
		if ($this -> request -> is('post')) {
			
			$update = $this -> data;
			$update['deleted']=0;
			$update['updateAt']= date("Y-m-d H:i:s"); 
			//$update['shortLink'] = $this->Link->getShortLink($update['name']);
			
			if ($update['img1']['name']!=""){
				$this->Files->deleteFile(WWW_ROOT.$update['imagePathOld1']);
				$update['img1'] = $this->Files->uploadFile(DIR_PRODUCT, $update['img1']['tmp_name'], IMG_PRODUCT, $update['img1']['name']);
			}else{
				$update['img1'] = $update['imagePathOld1'];
			}
			
			if ($update['img2']['name']!=""){
				$this->Files->deleteFile(WWW_ROOT.$update['imagePathOld2']);
				$update['img2'] = $this->Files->uploadFile(DIR_PRODUCT, $update['img2']['tmp_name'], IMG_PRODUCT, $update['img2']['name']);
			}else{
				$update['img2'] = $update['imagePathOld2'];
			}
			
			if ($update['icon']['name']!=""){
				$this->Files->deleteFile(WWW_ROOT.$update['iconOld']);
				$update['icon'] = $this->Files->uploadFile(DIR_PRODUCT, $update['icon']['tmp_name'], IMG_PRODUCT, $update['icon']['name']);
			}else{
				$update['icon'] = $update['iconOld'];
			}
			
			
			if($this -> Product -> save($update)){
				$this->Session->setFlash('Sảm phẩm "'.$update['name'].'" đã được cập nhật');
				$this->redirect(array('action' => 'index'));
			}
			$this -> set('errors', $this -> Product -> validationErrors);
		}
	}

	/**
	 * delete method
	 * param id integer , id of product
	 */
	public function admin_delete($id) {
		$conditions = array("Product.id" => $id);
		$data = $this->Product->find('first', array('conditions' => $conditions));
		parent::createException($data['Product']);
		$data['Product']['deleted'] = 1;
		$data['Product']['updateAt'] = date("Y-m-d H:i:s"); 
		if($this -> Product -> save($data)){
			$this->Session->setFlash('Sảm phẩm "'.$data['Product']['name'].'" đã bị xóa');
		}
		return $this->redirect(array('controller' => 'Products', 'action' => 'index'));
	}

}
?>